package cn.jiguang.jgadpubadapter.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import cn.jiguang.jgadpubadapter.CSJNativeAd;
import cn.jiguang.jgadpubadapter.JgNativeEntry;
import cn.jiguang.jgadpubadapter.JgPubAdInit;
import cn.jiguang.jgadpubadapter.LogUtil;
import cn.jiguang.jgssp.ad.ADJgNativeAd;
import cn.jiguang.jgssp.ad.data.ADJgNativeAdInfo;
import cn.jiguang.jgssp.ad.entity.ADJgAdSize;
import cn.jiguang.jgssp.ad.entity.ADJgExtraParams;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgNativeAdListener;
import xyz.adscope.amps.ad.nativead.adapter.AMPSNativeAdAdapterListener;
import xyz.adscope.amps.ad.nativead.adapter.AMPSNativeAdapter;
import xyz.adscope.amps.ad.nativead.inter.AMPSNativeAdExpressInfo;
import xyz.adscope.amps.common.AMPSError;
import xyz.adscope.amps.init.inter.IAMPSChannelInitCallBack;
import xyz.adscope.amps.inner.AMPSAdBiddingListener;
import xyz.adscope.amps.model.AMPSAdapterModel;
import xyz.adscope.amps.tool.util.AMPSScreenUtil;

public class JgPuAdNativeAdapter extends AMPSNativeAdapter {

    ADJgNativeAd adJgNativeAd;

    @Override
    public void startBid(Context context, AMPSAdapterModel ampsAdapterModel,
                         AMPSAdBiddingListener listener) {
        super.startBid(context, ampsAdapterModel, listener);
        initSDK();
    }

    @Override
    public void loadNetworkAd(Context context, AMPSAdapterModel ampsAdapterModel, AMPSNativeAdAdapterListener listener) {
        super.loadNetworkAd(context, ampsAdapterModel, listener);
        if (isBidding && adJgNativeAd != null) {
            JgPuAdNativeAdapter.this.onAdLoad();
            return;
        }
        initSDK();
    }

    private void initSDK() {
        JgPubAdInit.getInstance().initSDK(mInitAdapterConfig, new IAMPSChannelInitCallBack() {
            @Override
            public void successCallBack() {
                loadAd();
            }

            @Override
            public void failCallBack(AMPSError ampsError) {
                JgPuAdNativeAdapter.this.onAdFailed(ampsError.getCode(), ampsError.getMessage());
            }
        });
    }

    private List<ADJgNativeAdInfo> adJgNativeAdInfoList = null;


    private void loadAd() {

        ADJgExtraParams extraParams = new ADJgExtraParams.Builder()
                .adSize(new ADJgAdSize(AMPSScreenUtil.getScreenWidth(mContext), AMPSScreenUtil.getScreenHeight(mContext)))
                .build();

        adJgNativeAd = new ADJgNativeAd((Activity) mContext);
        adJgNativeAd.setLocalExtraParams(extraParams);
        adJgNativeAd.setListener(new ADJgNativeAdListener() {
            @Override
            public void onRenderFailed(ADJgNativeAdInfo adJgNativeAdInfo, ADJgError adJgError) {
                LogUtil.d(TAG, "onRenderFailed: ");

                JgPuAdNativeAdapter.this.onRenderFail(new AMPSError(String.valueOf(adJgError.getCode()),adJgError.getError()));
            }

            @Override
            public void onRenderSuccess(ADJgNativeAdInfo adJgNativeAdInfo) {
                LogUtil.d(TAG, "onRenderSuccess: ");
                JgPuAdNativeAdapter.this.onRenderSuccess();

            }

            @Override
            public void onAdReceive(List<ADJgNativeAdInfo> list) {
                Log.e(TAG, "onAdReceive: " + list.size());
                if (list == null || list.size() == 0) {
                    JgPuAdNativeAdapter.this.onAdFailed("", "list is null");
                    return;
                }
                adJgNativeAdInfoList = list;

                if (isBidding) {
                    int mEcpm = (int) list.get(0).getECPM();
                    JgPuAdNativeAdapter.this.onC2SBiddingSuccess(mEcpm);
                } else {
                    JgPuAdNativeAdapter.this.onAdLoad();
                }
            }

            @Override
            public void onAdExpose(ADJgNativeAdInfo adJgNativeAdInfo) {
                LogUtil.d(TAG, "onAdExpose: ");

                JgPuAdNativeAdapter.this.onAdShow();
                CSJNativeAd nativeItem = getJgNativeAdInfo(adJgNativeAdInfo);
                if (nativeItem != null && nativeItem.getAmpsListener()!=null) {
                    nativeItem.getAmpsListener().onAdShow();
                }
            }

            @Override
            public void onAdClick(ADJgNativeAdInfo adJgNativeAdInfo) {
                LogUtil.d(TAG, "onAdClick: ");
                JgPuAdNativeAdapter.this.onAdClicked();
                CSJNativeAd nativeItem = getJgNativeAdInfo(adJgNativeAdInfo);
                if (nativeItem != null && nativeItem.getAmpsListener()!=null) {
                    nativeItem.getAmpsListener().onAdClicked();
                }
            }

            @Override
            public void onAdClose(ADJgNativeAdInfo adJgNativeAdInfo) {
                LogUtil.d(TAG, "onAdClose: ");
                JgPuAdNativeAdapter.this.onAdDismiss();
                CSJNativeAd nativeItem = getJgNativeAdInfo(adJgNativeAdInfo);
                if (nativeItem != null && nativeItem.getAmpsListener()!=null) {
                    nativeItem.getAmpsListener().onAdClosed(nativeItem.getNativeExpressAdView());
                }
            }

            @Override
            public void onAdFailed(ADJgError adJgError) {
                if (adJgError != null) {
                    JgPuAdNativeAdapter.this.onAdFailed(String.valueOf(adJgError.getCode()), adJgError.getError());
                }
            }
        });
        LogUtil.d(TAG, "jgPub loadAd: " + adCount);
        adJgNativeAd.loadAd(mSpaceId, adCount);
    }

    List<AMPSNativeAdExpressInfo> ampsNativeAdExpressInfoList = new ArrayList<>();

    @Override
    public List<AMPSNativeAdExpressInfo> getNativeListInfo() {
        Log.e(TAG, "getNativeListInfo: ");
        for (int i = 0; i < adJgNativeAdInfoList.size(); i++) {
            bindAdListener(adJgNativeAdInfoList.get(i));
            int mEcpm = 0;
            if (isBidding) {
                mEcpm = (int) adJgNativeAdInfoList.get(i).getECPM();
            }

            AMPSNativeAdExpressInfo ampsNativeAdExpressInfo = new CSJNativeAd(adJgNativeAdInfoList.get(i), mContext,
                    this, mEcpm);
            ampsNativeAdExpressInfoList.add(ampsNativeAdExpressInfo);
        }
        Log.e(TAG, ":getNativeListInfo " + ampsNativeAdExpressInfoList.size());
        return ampsNativeAdExpressInfoList;
    }

    private void bindAdListener(ADJgNativeAdInfo adInfo) {

    }

    @Nullable
    private CSJNativeAd getJgNativeAdInfo(ADJgNativeAdInfo adJgNativeAdInfo) {
        Log.e(TAG, ":getJgNativeAdInfo ");
        List<AMPSNativeAdExpressInfo> list = getNativeListInfo();
        for (int i = 0; i < list.size(); i++) {
            AMPSNativeAdExpressInfo item = list.get(i);
            if (item instanceof CSJNativeAd) {
                if (((CSJNativeAd) item).getADJgInfo().equals(adJgNativeAdInfo)) {
                    return (CSJNativeAd) item;
                }
            }
        }
        return null;
    }

    private static final String TAG = "JgPuAdNativeAdapter";

    @Override
    public void showAd(Activity activity) {
        super.showAd(activity);
    }


    @Override
    public boolean isValid() {
        if (adJgNativeAd != null) {
            return false;
        }
        return true;
    }
}
